package com.fjec.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.common.view.ListItem;
import com.fjec.market.entity.InfoPcc;
import com.fjec.market.mapper.InfoPccMapper;
import com.fjec.market.service.IInfoPccService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.elasticsearch.common.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-10-30
 */
@Service
public class InfoPccServiceImpl extends ServiceImpl<InfoPccMapper, InfoPcc> implements IInfoPccService {

    @Resource
    private InfoPccMapper mapper;

    @Override
    public List<ListItem> getProv() {
        return this.getEntityListByLevelAndKey("1", null);
    }

    @Override
    public List<ListItem> getCity(String key) {
        return this.getEntityListByLevelAndKey("2", key);
    }

    @Override
    public List<ListItem> getCounty(String key) {
        return this.getEntityListByLevelAndKey("3", key);
    }

    private List<ListItem> getEntityListByLevelAndKey(String level, String key) {
        QueryWrapper<InfoPcc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("LevelCode", level);
        if (!Strings.isNullOrEmpty(key)) {
            queryWrapper.eq("ParentCode", key);
        }
        queryWrapper.orderByAsc("AreaCode");
        List<InfoPcc> entityList = mapper.selectList(queryWrapper);
        List<ListItem> resultList = new ArrayList<>();
        for (InfoPcc entity : entityList) {
            resultList.add(new ListItem(entity.getAreaCode(), entity.getAreaName()));
        }
        return resultList;
    }
}
